<template>
  <a-card :bordered="false" :class="'cust-erp-sub-tab'">
    <!-- 操作按钮区域 -->
    <div class="table-operator" v-if="mainId">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot='taskDes' slot-scope='text'>
          <j-ellipsis :value='text' :length='10' />
        </template>
        <template slot='incompleteCause' slot-scope='text'>
          <j-ellipsis :value='text' :length='10' />
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-if='planType==="1"' >编辑</a>
           <a @click="taskExaminer(record)" v-if='planType==="3"&&record.taskState==="1"'>检查</a>
          <a-divider type="vertical" v-if='planType==="1"||(planType==="3"&&record.taskState==="1")' />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-if='planType==="1"'>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a style="color:red">删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <hfPmProjectTasks-modal ref="modalForm" @ok="modalFormOk" :mainId="mainId" :projectId ="projectId"></hfPmProjectTasks-modal>
  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import HfPmProjectTasksModal from './modules/HfPmProjectTasksModal'
  import { deleteAction, getAction } from '@api/manage'
  import store from '@/store'

  export default {
    name: "HfPmProjectTasksList",
    mixins:[JeecgListMixin],
    components: { HfPmProjectTasksModal },
    props:{
      mainId:{
        type:String,
        default:'',
        required:false
      },
      planType:{
        type:String,
        default:'',
        required:false
      },
      projectId:{
        type:String,
        default:'',
        required:false
      }
    },
    watch:{
      mainId:{
        immediate: true,
        handler(val) {
          if(!this.mainId){
            this.clearList()
          }else{
            this.queryParam['planId'] = val
            this.loadData(1);
          }
        }
      },

    },
    data () {
      return {
        description: '双周计划表管理页面',
        disableMixinCreated:true,
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'专题名称',
            align:"center",
            dataIndex: 'subjectName'
          },
          {
            title:'目标名称',
            align:"center",
            dataIndex: 'targetName'
          },
          {
            title:'任务描述',
            align:"center",
            dataIndex: 'taskDes',
            scopedSlots: { customRender: 'taskDes' },
            width: 300
          },
          {
            title:'任务状态',
            align:"center",
            dataIndex: 'taskState_dictText',
          },
          {
            title:'责任人',
            align:"center",
            dataIndex: 'dutyPersionId_dictText'
          },
          {
            title:'检查人',
            align:"center",
            dataIndex: 'examiner'
          },
          {
            title:'未完成原因',
            align:"center",
            dataIndex: 'incompleteCause',
            scopedSlots: { customRender: 'incompleteCause' },
            customRender: function(text) {
              return !text ? '---' : text
            }
          },
          {
            title:'创建时间',
            align:"center",
            dataIndex: 'createTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/pm/hfPmDoubleWeekPlan/listHfPmProjectTasksByMainId",
          delete: "/pm/hfPmDoubleWeekPlan/deleteHfPmProjectTasks",
          deleteBatch: "/pm/hfPmDoubleWeekPlan/deleteBatchHfPmProjectTasks",
          exportXlsUrl: "/pm/hfPmDoubleWeekPlan/exportHfPmProjectTasks",
          importUrl: "/pm/hfPmDoubleWeekPlan/importHfPmProjectTasks",
          changeTaskStatus: 'pm/hfPmDoubleWeekPlan/changeTaskStatus'
        },
        dictOptions:{
         subjectId:[],
         planType:[],
        },
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl(){
        return `${window._CONFIG['domianURL']}/${this.url.importUrl}/${this.mainId}`;
      }
    },
    methods: {
      clearList(){
        this.dataSource=[]
        this.selectedRowKeys=[]
        this.ipagination.current = 1
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'deleteFlag',text:'删除标记',dictCode:''})
        fieldList.push({type:'popup',value:'projectId',text:'项目简称', popup:{code:'hf_pm_project',field:'short_name',orgFields:'short_name',destFields:'project_id'}})
        fieldList.push({type:'string',value:'subjectId',text:'专题名称',dictCode:''})
        fieldList.push({type:'string',value:'targetId',text:'目标名称',dictCode:''})
        fieldList.push({type:'string',value:'planType',text:'计划类型',dictCode:''})
        fieldList.push({type:'date',value:'startTime',text:'开始时间'})
        fieldList.push({type:'date',value:'endTime',text:'结束时间'})
        this.superFieldList = fieldList
      },

      handleEdit: function (record) {
        if(record.taskState == "4"||record.taskState == "3"||this.planType=="2"){
          this.$message.error('请选择未开始状态的任务或者未归档的计划进行编辑!');
          return;
        }
        let isAdd ='2';
        this.$refs.modalForm.isAdd = isAdd;
        this.$refs.modalForm.planType = this.planType;
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
        this.$refs.modalForm.disableSubmit = false;
      },
      handleDetail:function(record){
        record.editType = 'view';
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title="详情";
        this.$refs.modalForm.disableSubmit = true;
      },
      handleAdd: function () {
        if(this.planType == "2") {
          this.$message.error('请选择未归档的计划进行新增!');
          return;
        }
        let record = {
          dutyPersionId: store.getters.userInfo.realname,
          taskState: '1'
        }
        this.$refs.modalForm.isAdd = '2';
        this.$refs.modalForm.addRecord(record);
        this.$refs.modalForm.title = "新增";
        this.$refs.modalForm.disableSubmit = false;
      },
      handleDelete: function (id) {
        if(this.planType === "3"){
          this.$message.error('已发布的计划的任务不可删除!');
          return;
        }
        if(!this.url.delete){
          this.$message.error("请设置url.delete属性!")
          return
        }
        var that = this;
        deleteAction(that.url.delete, {id: id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      //任务检查
      taskExaminer:function(record){
        if(this.planType == "2"){
          this.$message.error('该任务已检查完成!');
          return;
        }
        //3:计划状态为已发布，只能修改任务的状态，其他信息不可编辑
        record.examiner=store.getters.userInfo.realname;
        this.$refs.modalForm.isAdd = '1';
        this.$refs.modalForm.planType = this.planType;
        // this.$refs.modalForm.examiner =store.getters.userInfo.realname;
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "检查";
        this.$refs.modalForm.disableSubmit = false;
      },
      taskEnd:function(record){
        if('2' != record.taskState){
          this.$message.error('请操作进行中的任务!');
          return;
        }
        record.changeTaskStatus = '4';
        getAction(this.url.changeTaskStatus, record).then((res) => {
          if (res.success) {
            this.loadData()
            this.$message.success("任务已经完成。")
          } else {
            this.$message.warning("任务完成操作失败！")
          }
        })
      },
      taskEx:function(record){
        if('2' != record.taskState){
          this.$message.error('请操作进行中的任务!');
          return;
        }
        record.changeTaskStatus = '3';
        getAction(this.url.changeTaskStatus, record).then((res) => {
          if (res.success) {
            this.loadData()
            this.$message.success("任务状态已经修改为异常。")
          } else {
            this.$message.warning("任务异常处理失败！")
          }
        })
      },
      taskClose:function(record){
        if (!this.url.changeTaskStatus) {
          this.$message.error('请设置url.changeTaskStatus属性!')
        }
        getAction(this.url.changeTaskStatus, record).then((res) => {
          if (res.success) {
            this.loadData()
            this.$message.success(res.message)
          } else {
            this.$message.warning(res.message)
          }
        })
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
